package interviewClassic150;

public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        StringBuilder sb = new StringBuilder();
        //计算所有字符串中的最小长度
        int minLen = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            minLen = Math.min(minLen,strs[i].length());
        }
        for (int i = 0; i < minLen; i++) {
            char cur = strs[0].charAt(i);
            //遍历字符串数组
            for (int j = 1; j < strs.length; j++) {
                if (strs[j].charAt(i) != cur) {
                    //到这个位置已经不是公共的，直接将之前公共的返回
                    return sb.toString();
                }
            }
            sb.append(cur);
        }
        return sb.toString();
    }
}
